const routes = [
  {
    path: '/',
    component: 'layout/index',
    redirect: '/dashboard',
    children: [
      {
        path: '/dashboard',
        component: 'views/dashboard/index',
        name: 'Dashboard',
        meta: { title: 'Dashboard', icon: 's-grid', affix: true }
      }
    ]
  },
  {
    path: '/user',
    component: 'layout/Layout',
    redirect: '/user/list',
    name: 'User',
    meta: {
      title: 'User',
      icon: 'user',
      roles: ['admin'] // 只有管理员可访问
    },
    children: [
      {
        path: '/user/list',
        component: 'views/user/list',
        name: 'UserList',
        meta: {
          title: 'User List',
          roles: ['admin'] // 只有管理员可访问
        }
      },
      {
        path: '/user/create-edit',
        component: 'views/user/create-edit',
        name: 'UserCreateEdit',
        meta: {
          title: 'Create User',
          roles: ['admin'] // 只有管理员可访问
        }
      }
    ]
  },
  {
    path: '/login',
    component: 'views/login/index',
    hidden: true
  },
  // ... 可能还有其他路由配置，比如404页面等
  {
    path: '*',
    redirect: '/404',
    hidden: true
  }
]

function addRoutes(routes) {
  return routes.map((route) => {
    // 递归处理子路由
    if (route.children) {
      route.children = addRoutes(route.children)
    }

    // 修改 component 字段
    if (
      typeof route.component === 'string' &&
      route.component.startsWith('views/')
    ) {
      route.component = `@/${route.component}.vue`
    }
    if (
      typeof route.component === 'string' &&
      route.component.startsWith('layout/index')
    ) {
      route.component = `@/${route.component}.vue`
    }

    return route
  })
}

// 调用 addRoutes 函数处理 routes 数组
const updatedRoutes = addRoutes(routes)

// 输出更新后的路由数组以验证结果
console.log(JSON.stringify(updatedRoutes))
